﻿using System.Collections.Generic;
using System.Linq;
using LuceneSearch.Model;
using System.Data.SqlClient;
using System;
using System.Diagnostics;

namespace LuceneSearch.Data
{
    public static class SampleDataRepository
    {
        public static SampleData Get(int id)
        {
            return GetAll().SingleOrDefault(x => x.Id.Equals(id));
        }

        public static List<SampleData> GetAll()
        {
            try
            {
                List<SampleData> data = new List<SampleData>();

                SqlConnection thisConnection = new SqlConnection();
                thisConnection.ConnectionString = "Server=localhost\\SQLEXPRESS;" + "DataBase=WebCrawler;" + "User Id=user;" + "Password=user;";
                thisConnection.Open();
                Debug.WriteLine("OK SQL");
                SqlCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = "SELECT Id, Name, Description FROM data";
                SqlDataReader thisReader = thisCommand.ExecuteReader();
                while (thisReader.Read())
                {
                    SampleData sample_data = new SampleData();
                    sample_data.Id = int.Parse(thisReader["Id"].ToString());
                    sample_data.Name = thisReader["Name"].ToString();
                    sample_data.Description = thisReader["Description"].ToString();

                    data.Add(sample_data);
                }
                thisReader.Close();
                thisConnection.Close();
                return data;
            }
            catch (SqlException e)
            {
                Debug.WriteLine("KO SQL");
                //Console.WriteLine(e.ToString());
                return new List<SampleData>();
            }

        }
    }
}